"""author: wangxin
   data: 2021/12/12
   project: cjh.py
"""


# 1、使用类实现迭代器，实现斐波拉契数列
# 把一个类作为一个迭代器使用需要在类中实现两个方法 iter() 与 next()

class Fblq():
    def __init__(self, n):      # n为生成数列的个数
        self.n = n
        self.count = 0
        self.a = 0
        self.b = 1

    def __iter__(self):
        return self

    def __next__(self):
        if self.count < self.n:
            self.a, self.b = self.b, self.a + self.b
            self.count += 1
            return self.a
        else:
            raise StopIteration


for i in Fblq(10):
    print(i, end=" ")
